﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YNCNumberSys.db;
using YNCNumberSys.ZJClass;

namespace YNCNumberSys.DbDal
{
    public class Apply4CNumberDal
    {
        private CommandManager commandManager = new CommandManager();
        private ConnectionManager connectionManager = new ConnectionManager();
        public DataTable BuildFiles(string filesFolder)
        {
            DirectoryInfo folder = new DirectoryInfo(filesFolder);
            if (folder.Exists)
            {
                FileInfo[] files = folder.GetFiles();
                Array.Sort(files, new ZJFileNameSort());
                return YNCNumberMethod.GetFileInfo(files,false).Copy();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 是否是一种图纸类型。
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public bool IsOneClassTzlx(DataTable dt)
        {
            DataView dataView = dt.DefaultView;
            System.Data.DataTable distinct = dataView.ToTable(true, "图纸类型");
            if (distinct.Rows.Count == 1)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 执行一次SQL。
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="sql">如string.Format("图纸序号='ZP'")</param>
        /// <returns></returns>
        public DataRow[] ExecuteScalar(DataTable dt,string sql)
        {
            return dt.Select(sql);
        }
        /// <summary>
        /// 获取图纸的统计信息。
        /// </summary>
        /// <param name="dtOrigin">数据源。</param>
        /// <returns>图纸的统计信息表。</returns>
        public DataTable GetPaperStatic(DataTable dtOrigin)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("图纸分类");
            dt.Columns.Add("数量");
            dt.Columns.Add("图纸类型");
            System.Data.DataTable distinct=dtOrigin.DefaultView.ToTable(true, "图纸大小");
            foreach (DataRow dr1 in distinct.Rows)
            {
                DataRow dr = dt.NewRow();
                dr["图纸分类"] = "A" + dr1["图纸大小"].ToString();
                dr["数量"] = dtOrigin.Select("图纸大小='" + dr1["图纸大小"].ToString() + "'").Length;
                dr["图纸类型"] = dtOrigin.Rows[0]["图纸类型"];
                dt.Rows.Add(dr);
            }
            return dt;
        }
        
        public DataTable CheckFileNo(string machineNo,
            string aNo,
            string usm)
        {
            string sql = string.Format(@"exec [dbo].[YNC_AllocateFileNoCheck] '{0}','{1}','{2}'",
  machineNo, aNo, usm);
            return commandManager.ExecuteNonQuery(connectionManager.getConnection(), sql, @"图号检查");
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="machineNo">机床号</param>
        /// <param name="aNo">A号，暂时没用</param>
        /// <param name="paperSeries">图纸系列，如4C</param>
        /// <param name="userCode">用户号</param>
        /// <param name="wx">1，外协，0 非外协</param>
        /// <param name="rang">图号的个数</param>
        /// <returns></returns>
        public DataTable ExecuteStoredProcedure(
            string machineNo,
            string aNo,
            string partName, 
            string spec,
            string usm,
            string auditor,
            int wx,
            string wxDw)
        {
            string sql = string.Format(@"exec [dbo].[YNC_AllocateFileNo] '{0}','{1}','{2}','{3}',{4},'{5}',{6},{7},{8},'4C'",
                machineNo, 
                aNo,
                partName,
                spec,
                usm,
                auditor,
                1,
                wx,
                1,
                wxDw);
            return commandManager.ExecuteNonQuery(connectionManager.getConnection(), sql,@"图号");
        }

        /// <summary>
        /// 执行存储过程，从最大已经分配的图纸号。
        /// </summary>
        /// <param name="machineNo">机床号</param>
        /// <param name="aNo">A号，暂时没用</param>
        /// <param name="paperSeries">图纸系列，如4C</param>
        /// <param name="userCode">用户号</param>
        /// <param name="wx">1，外协，0 非外协</param>
        /// <param name="rang">图号的个数</param>
        /// <returns></returns>
        public DataTable ExecuteStoredProcedureMaxIncrease(
            string machineNo,
            string aNo,
            string partName,
            string spec,
            string usm,
            string auditor,
            int wx,
            string wxDw)
        {
//            [dbo].[YNC_AllocateFileNoMaxIncrease]
                    //@machineNo nvarchar(50)='' 0,        --设置默认值
                    //@aNo    nvarchar(50)='' 1,             --设置默认值
                    //@partName nvarchar(50)='' 2,  
                    //@partSpec nvarchar(50)='' 3, 
                    //@usm nvarchar(50) 4,              --设置默认值
                    //@auditor nvarchar(50)5,            --审核人
                    //@protect int=1 6,
                    //@wx int=0 7,
                    //@fc int=0 8,--4C 1,其他0
                    //@wxDw nvarchar(max)=''
            string sql = string.Format(@"exec [dbo].[YNC_AllocateFileNoMaxIncrease] '{0}','{1}','{2}','{3}',{4},'{5}',{6},{7},{8},'4C'",
                machineNo, 
                aNo, 
                partName,
                spec, 
                usm,
                auditor,
                1,
                wx,
                1, //--4C 1, 其他0,1代表4C  XSL ADD
                wxDw);
            return commandManager.ExecuteNonQuery(connectionManager.getConnection(), sql, @"图号");
        }
    }
}
